package bsh.dyn;

/**
 * An {@link ActualParameter} that is a reference (as oppose to a primitive).
 * 
 * @author takeshi
 */
public class ReferenceParameter extends AbstractReferenceParameter implements ActualParameter {

    private final Class<?> javaType;

    /**
     * C'tor
     * 
     * @param javaType Class
     */
    public ReferenceParameter(Class<?> javaType) {
        super();
        this.javaType = javaType;
    }

    /**
     * {@inheritDoc}
     */
    public Class<?> getJavaType() {
        return this.javaType;
    }

    /**
     * {@inheritDoc}
     */
    public boolean isNull() {
        return false;
    }

    /**
     * {@inheritDoc}
     */
    public ActualParameter[] getDeclaredInterfaces() {
        Class<?>[] interfaces = this.javaType.getInterfaces();
        if (interfaces == null) {
            return new ActualParameter[0];
        }
        ActualParameter[] params = new ActualParameter[interfaces.length];
        for (int i = 0; i < params.length; i++) {
            params[i] = new ReferenceParameter(interfaces[i]);
        }
        return params;
    }

    /**
     * {@inheritDoc}
     */
    public ActualParameter getSuperType() {
        if (this.javaType == Object.class || this.javaType.getSuperclass() == null) {
            return null;
        }
        return new ReferenceParameter(this.javaType.getSuperclass());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((this.javaType == null) ? 0 : this.javaType.hashCode());
        return result;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (this.getClass() != obj.getClass()) {
            return false;
        }
        ReferenceParameter other = (ReferenceParameter) obj;
        if (this.javaType == null) {
            if (other.javaType != null) {
                return false;
            }
        } else if (!this.javaType.equals(other.javaType)) {
            return false;
        }
        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return "ReferenceParameter [javaType=" + javaType + "]";
    }

}
